package com.dl7.player.utils;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;

/**
 * Created by long on 2016/11/4.
 */

public final class AnimHelper {

    private AnimHelper() {
        throw new AssertionError();
    }


    /**
     * 执行从右滑入动画
     *
     * @param component
     * @param startX
     * @param endX
     * @param duration
     */
    public static void doSlideRightIn(Component component, int startX, int endX, int duration) {
        AnimatorProperty translationX = new AnimatorProperty();
        translationX.moveFromX(startX);
        translationX.moveToX(endX);
        AnimatorProperty alpha = new AnimatorProperty();
        alpha.alphaFrom(0);
        alpha.alpha(1);
        AnimatorGroup animatorGroup = new AnimatorGroup();
        animatorGroup.setDuration(duration);
        animatorGroup.runParallel(translationX, alpha);
        animatorGroup.start();
    }

    /**
     * 裁剪视图宽度
     *
     * @param component
     * @param srcWidth
     * @param endWidth
     * @param duration
     */
    public static void doClipViewWidth(final Component component,
                                       int srcWidth, int endWidth, int duration) {
        MyValueAnimator myValueAnimator =
                MyValueAnimator.ofFloat((float) srcWidth, (float) endWidth);
        myValueAnimator.setDuration(duration);

        myValueAnimator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                ComponentContainer.LayoutConfig layoutConfig = component.getLayoutConfig();
                layoutConfig.width = (int) v;
                component.setLayoutConfig(layoutConfig);
            }
        });
        myValueAnimator.setCurveType(Animator.CurveType.ACCELERATE);
        myValueAnimator.start();
    }

    /**
     * 裁剪视图宽度
     *
     * @param component
     * @param srcHeight
     * @param endHeight
     * @param duration
     */
    public static void doClipViewHeight(final Component component,
                                        int srcHeight, int endHeight, int duration) {
        MyValueAnimator myValueAnimator = MyValueAnimator.ofFloat((float) srcHeight, (float) endHeight);
        myValueAnimator.setDuration(duration);
        myValueAnimator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                ComponentContainer.LayoutConfig layoutConfig = component.getLayoutConfig();
                layoutConfig.height = (int) v;
                component.setLayoutConfig(layoutConfig);
            }
        });
        myValueAnimator.setCurveType(Animator.CurveType.ACCELERATE);
        myValueAnimator.start();
    }
}
